package com.craftplay.service;

import com.craftplay.VO.CommentVO;
import com.craftplay.dao.CommentMapper;
import com.craftplay.dao.UnitMapper;
import com.craftplay.model.Comment;
import com.craftplay.model.Unit;
import com.craftplay.model.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UserService userService;

    @Autowired
    private UnitMapper unitMapper;

    public int insertOneRecord(Comment comment) {
        return commentMapper.insert(comment);
    }

    public int deleteCommentByCommentId(Long commentId) {
        return commentMapper.deleteByPrimaryKey(commentId);
    }

    public List<Comment> selectCommentByUnitId(int parentId) {
        return commentMapper.selectByUnitId(parentId);
    }

    public List<Comment> selectCommentByParentId(Long parentId) {
        return commentMapper.selectByParentId(parentId);
    }

    public List<Comment> getCommentList(Comment comment) {
        return commentMapper.getCommentList(comment);
    }


    public List<CommentVO> getCommentList(int unitId, int thisUserId) {
        List<Comment> commentList = selectCommentByUnitId(unitId);
        List<CommentVO> commentVOList = new ArrayList<CommentVO>();
        if (commentList.size() != 0) {
            for (Comment comment : commentList) {
                CommentVO commentVO = new CommentVO();
                BeanUtils.copyProperties(comment, commentVO);
                Long currentCommentId = comment.getId();
                int currentUserId = comment.getFromId();//评论者的ID
                if (comment.getIsPrivate() == 1 && (thisUserId != comment.getFromId() || thisUserId != comment.getToId())) {
                    //私密评论  只有评论者和被评论人可以看见
                    continue;
                }
                User user = userService.selectUserById(currentUserId);
                if (user != null) {
                    commentVO.setNickName(user.getNickname());
                    commentVO.setUserHeadUrl(user.getHeadUrl());
                    commentVO.setSex(user.getSex() == null ? user.getSex() : 0);
                }
                List<Comment> subCommentList = selectCommentByParentId(currentCommentId);
                List<CommentVO> subComentVOList = new ArrayList<CommentVO>();
                for (Comment subComment : subCommentList) {
                    CommentVO subCommentVO = new CommentVO();
                    BeanUtils.copyProperties(subComment, subCommentVO);
                    int subcurrentUserId = subComment.getFromId();
                    User subUser = userService.selectUserById(subcurrentUserId);
                    if (subUser != null) {
                        subCommentVO.setNickName(subUser.getNickname());
                        subCommentVO.setUserHeadUrl(subUser.getHeadUrl());
                        subCommentVO.setSex(subUser.getSex() != null ? subUser.getSex() : 0);
                    }
                    subComentVOList.add(subCommentVO);
                }
                User toUserId = userService.selectUserById(comment.getToId());
                if (toUserId != null) {
                    commentVO.setToNickName(toUserId.getNickname());
                }
                commentVO.setCommentVOList(subComentVOList);
                commentVOList.add(commentVO);
            }
            return commentVOList;
        }
        return commentVOList;
    }

    /**
     * 获取有未读信息的活动
     *
     * @param thisUserId
     */
    public List<Unit> getNotReadComment(int thisUserId) {
        List<Comment> commentList = commentMapper.getNotReadComment(thisUserId);
        List<Integer> unitContainList = new ArrayList<>();
        List<Unit> unitList = new ArrayList<>();
        Unit commentUnitId = null;
        for (Comment comment : commentList) {
            //父节点对应的就是活动
            if (comment.getIsRoot() != 1) {
                commentUnitId = getCommentUnitId(comment.getParentId());
            } else {
                commentUnitId = unitMapper.selectByPrimaryKey(comment.getParentId().intValue());
            }
            if (commentUnitId == null) {
                continue;
            }
            if (!unitContainList.contains(commentUnitId.getId())) {
                unitContainList.add(commentUnitId.getId());
                commentUnitId.setNotReadComment(1);
                unitList.add(commentUnitId);
            } else {
                unitList.get(unitContainList.indexOf(commentUnitId.getId())).setNotReadComment(unitList.get(unitContainList.indexOf(commentUnitId.getId())).getNotReadComment() + 1);
            }
        }
        return unitList;

    }

    /**
     * 获得改评论对应的活动
     *
     * @param parentId
     * @return
     */
    public Unit getCommentUnitId(long parentId) {
        Comment comment = commentMapper.selectByPrimaryKey(parentId);
        if (comment.getIsRoot() == 1) {
            return unitMapper.selectByPrimaryKey(comment.getParentId().intValue());
        } else {
            getCommentUnitId(comment.getParentId());
        }
        return null;
    }

    /**
     * 获取活动详情中的评论
     *
     * @param unitId     活动详情Id
     * @param thisUserId 请求的用户
     * @return
     */
    public List<CommentVO> getUnitDetailComment(int unitId, int thisUserId) {
        List<Comment> commentList = selectCommentByUnitId(unitId);
        List<CommentVO> commentVOList = new ArrayList<>();
        if (commentList.size() != 0) {
            for (Comment comment : commentList) {
                CommentVO commentVO = new CommentVO();
                BeanUtils.copyProperties(comment, commentVO);
                Long currentCommentId = comment.getId();
                int currentUserId = comment.getFromId();//评论者的ID
                if (comment.getIsPrivate() == 1 && (thisUserId != comment.getFromId() || thisUserId != comment.getToId())) {
                    //私密评论  只有评论者和被评论人可以看见
                    continue;
                }
                User user = userService.selectUserById(currentUserId);
                if (user != null) {
                    commentVO.setNickName(user.getNickname());
                    commentVO.setUserHeadUrl(user.getHeadUrl());
                    commentVO.setSex(user.getSex());
                }
                List<Comment> subCommentList = selectCommentByParentId(currentCommentId);
                List<CommentVO> subComentVOList = new ArrayList<>();
                for (Comment subComment : subCommentList) {
                    CommentVO subCommentVO = new CommentVO();

                    //开始的评论不是
                    if (subComment.getIsRoot() == 1) {
                        continue;
                    }

                    if (subComment.getIsPrivate() == 1 && (thisUserId != subComment.getFromId() || thisUserId != subComment.getToId())) {
                        //私密评论  只有评论者和被评论人可以看见
                        continue;
                    }


                    BeanUtils.copyProperties(subComment, subCommentVO);
                    int subcurrentUserId = subComment.getFromId();
                    User subUser = userService.selectUserById(subcurrentUserId);
                    if (subUser != null) {
                        subCommentVO.setNickName(subUser.getNickname());
                        subCommentVO.setUserHeadUrl(subUser.getHeadUrl());
                        subCommentVO.setSex(subUser.getSex());
                    }
                    subComentVOList.add(subCommentVO);

                    //如果该评论是给自己的设为已读
                    if (subComment.getToId() == thisUserId) {
                        commentMapper.setIsRead(subComment.getId());
                    }

                }
                commentVO.setCommentVOList(subComentVOList);
                commentVOList.add(commentVO);

                //如果该评论是给自己的设为已读
                if (comment.getToId() == thisUserId) {
                    commentMapper.setIsRead(comment.getId());
                }

            }
            return commentVOList;
        }
        return commentVOList;
    }
}
